<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/16 0016
 * Time: 11:03
 */

namespace app\api\controller\v1\cleaner;


use app\api\controller\v1\Cleaner;
use library\tools\Data;
use think\Db;

class Auth extends Cleaner
{
    /**
     * 查询实名认证状态
     */
    public function index()
    {
        $status = -1;
        $auth = [];
        if($this->cleaner->auth_state == '1'){
            $status = 1;
            $auth = Db::name('CleanerAuth')->where('cid','eq',$this->cid)->find();
        } else {
            if(Db::name('CleanerAuth')->where([['cid','eq',$this->cid],['status','egt','0']])->count() > 0){
                $status = 0;
            }
        }
        $this->success('success',['status' => $status, 'auth' => $auth]);
    }

    /**
     * 提交实名认证
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function save()
    {
        //如果已提交过实名认证审核 或者审核中 则不能继续提交
        if($this->cleaner->auth_state == '1'){
            $this->error('您已通过实名认证！');
        }
        if(Db::name('CleanerAuth')->where([['cid','eq',$this->cid],['status','egt','0']])->count() > 0)
        {
            $this->error('您的实名认证正在审核中！');
        }
        $data = $this->_input([
            'real_name' => $this->request->post('real_name'),
            'id_card_number' => $this->request->post('id_card_number'),
            'id_card_front' => $this->request->post('id_card_front'),
            'id_card_other' => $this->request->post('id_card_other'),
            'id_card_hold' => $this->request->post('id_card_hold')
        ],[
            'real_name' => 'require',
            'id_card_number' => 'require|idCard',
            'id_card_front' => 'require|url',
            'id_card_other' => 'require|url',
            'id_card_hold' => 'require|url'
        ],[
            'real_name.require' => '请输入真实姓名！',
            'id_card_number.require' => '请输入身份证号码！',
            'id_card_number.idCard' => '身份证号码格式错误！',
            'id_card_front.require' => '请上传身份证正面照片！',
            'id_card_front.url' => '身份证正面照片格式错误！',
            'id_card_other.require' => '请上传身份证反面照片！',
            'id_card_other.url' => '身份证反面照片格式错误！',
            'id_card_hold.require' => '请上传手持身份证照片！',
            'id_card_hold.url' => '手持身份证照片格式错误！',
        ]);
        $data['cid'] = $this->cid;
        $data['status'] = '0';
        Data::save('CleanerAuth',$data,'cid');
        $this->success('实名认证提交成功！');
    }

}